﻿/// <reference path="../scripts/jquery-1.11.2.min.js" />
/// <reference path="../scripts/bootstrap.min.js" />

if (typeof ($) != "undefined" && $) {
    //验证用户
    CheckUser();
    $(document).ready(onReady);
}

function onReady() {
    $("#header").click(function () {
        window.location.href = "index.html";
    });

    $('#loadingToast').show();
    loadData('wf', 'wfList');
    loadData('yf', 'yfList');
    loadData('wc', 'wcList');
    loadData('tk', 'tkList');
    $('#loadingToast').hide();
}

function loadData(state, liname) {
    var data = { state: state };
    $.ajax({
        type: "get",
        url: "api/order.ashx?ac=list&k=" + Math.random(),
        data: data,
        success: function (data) {
            var list = eval("(" + data + ")");
            $("#" + liname + "").children().remove();
            for (var i = 0; i < list.length; i++) {
                var o = list[i];
                var htm = '<div class="weui_cells" style="margin-top:10px;" id="div' + o.OrderID + '">';
                htm += '<div class="weui_cell">';
                htm += '<div class="weui_cell_bd weui_cell_primary">订单号：' + o.OrderNo + '</div>';
                htm += '<div class="weui_cell_no" lang="state">' + o.StateName + '</div>';
                htm += '</div>';
                htm += '<div class="weui_cell">';
                htm += '<div class="weui_cell_bd weui_cell_primary fz13 fc6">';
                htm += '车辆：' + o.CarType + ' [' + o.CarNumber + ']<br />';
                if (o.Address.length > 12) {
                    htm += '位置：' + o.Address.substring(0, 12) + '<br />';
                    htm += o.Address.substring(12) + '<br />';
                }
                else {
                    htm += '位置：' + o.Address + '<br />';
                }
                htm += '合计：￥' + o.Price;
                htm += o.Interior == 1 ? ' (外观+内饰简洗)' : ' (外观)';
                htm += '<br />';
                if (o.CouType == '1') {
                    htm += '优惠：洗车券<br />';
                }
                else if (o.CouponsMoney != 'null' && Number(o.CouponsMoney) > 0) {
                    htm += '优惠：-￥' + o.CouponsMoney + '<br />';
                }
                if (o.Cash != 'null' && Number(o.Cash) > 0) {
                    htm += '余额：-￥' + o.Cash + '<br />';
                }
                htm += '实付：￥' + o.Pay + '<br />';
                if (o.State == '0') {
                    htm += '下单时间：' + (o.AddDate == null ? "" : o.AddDate);
                }
                else if (o.State != '9') {
                    htm += '支付时间：' + (o.PayDate == null ? "" : o.PayDate);
                }
                htm += '</div>';
                if (o.State == '4' || o.State == '2') {
                    htm += '<div class="weui_cell_ft fz13 fc6">' + o.UserName + '(工号：' + padLeft(o.UserID, 4) + ')<br />手机：<a href="tel:' + o.UserMobile + '" class="fz15">' + o.UserMobile + '</a><br />开始时间：<br />' + o.StartTime + '</div>';
                }
                else if (o.State == '3') {
                    htm += '<div class="weui_cell_ft fz13 fc6">' + o.UserName + '(工号：' + padLeft(o.UserID, 4) + ')<br />手机：<a href="tel:' + o.UserMobile + '" class="fz15">' + o.UserMobile + '</a><br />开始时间：<br />' + o.StartTime + '<br />完成时间：<br />' + o.EndTime + '</div>';
                }
                htm += '</div>';
                htm += '<div class="weui_cell">';
                htm += '<div class="weui_cell_bd weui_cell_primary fz13 fc6">';
                if (o.WaitTip != '') {
                    htm += o.WaitTip;
                }
                htm += '</div>';
                htm += '<div class="weui_cell_no">';
                if (o.State == '0') {
                    htm += '<button type="button" class="btn btn-success btn-sm" onclick="Pay(' + o.OrderID + ')">付款</button> ';
                }
                if (o.State == '0' || o.State == '1') {
                    htm += '<button type="button" class="btn btn-warning btn-sm" onclick="Cancel(' + o.OrderID + ',' + o.CouponsID + ')">取消</button> ';
                }
                //                if (o.State == '3') {
                //                    htm += '<button type="button" class="btn btn-info btn-sm" onclick="Comment(' + o.OrderID + ')">评价</button> ';
                //                }
                htm += '</div>';
                htm += '</div>';
                htm += '</div>';

                $("#" + liname + "").append(htm);
            }

            if (list.length == 0) {
                $("#" + liname + "").append('<div class="width100 center"><img src="images/none.png" /></div>');
            }
        }
    });
}

//付款
function Pay(id) {
    window.parent.location.href = "JsApiPayPage.aspx?OrderID=" + id;
}
//取消
function Cancel(id, couponsid) {
    if (confirm("您确定要取消订单吗？")) {
        var data = { id: id, couponsid: couponsid };
        $.ajax({
            type: "post",
            data: data,
            url: "api/order.ashx?ac=cancel&k=" + Math.random(),
            beforeSend: function () {
                $('#loadingToast').show();
            },
            error: function () {
                showToast("操作失败");
            },
            success: function (data) {
                var obj = eval("(" + data + ")").result;
                if (obj.r == "suc") {
                    loadData('wf', 'wfList');
                    loadData('yf', 'yfList');
                    loadData('wc', 'wcList');
                    loadData('tk', 'tkList');
                    showToast(obj.val);
                }
                else {
                    loadData('wf', 'wfList');
                    loadData('yf', 'yfList');
                    showToast(obj.val);
                }
            },
            complete: function () {
                $('#loadingToast').hide();
            }
        });
    }
}
//评价
function Comment(id) {
    alert(id);
}